package cn.edu.scau.cmi.zhengweibo.composite.safe;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.edu.scau.cmi.zhengweibo.composite.consistent.ConsistentComponent;

public class SafeComposite  extends SafeComponent{
	
	public SafeComposite(String name) {
		super(name);
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void show(String str) {
		System.out.println(str+"["+this.name+"]");
		if(this.child!=null) {
			str+=" ";
			for(SafeComponent s : child) {
				s.show(str);
			}
		}
	}
	
	public String show1(String str) {
		String newStr = str;
		newStr+="["+this.name+"]";
		//System.out.println(str+"["+this.name+"]");
		if(this.child!=null) {
			str+=" ";
			for(SafeComponent s : child) {
				newStr +="\n"+s.show1(str);
			}
		}
		return newStr;
	}
	
	public void add(SafeComponent component) {
		child.add(component);
	}
	public void remove(SafeComponent component) {
		child.remove(component);
	}

	public SafeComponent find(String name) {
		// TODO Auto-generated method stub
		for(SafeComponent s : child) {
			if(s.getName().equals(name)){
				return s;
			}else if(s.child!=null) {
				for(SafeComponent c : s.child) {
					return c.find(name);
				}
			}
		}
		return null;
	}
	
}